<template>
  <div style="padding: 10px; width: 100%">
    <div style="margin: 10px 0; width: 600px; float: left">
      <el-input
        placeholder="请输入班主任姓名"
        v-model="search"
        style="width: 200px"
      >
        <template #append>
          <el-button icon="el-icon-search" @click="load"></el-button>
        </template>
      </el-input>

      <span style="margin-left: 20px">年级：</span>
      <el-select
        v-model="gradeId"
        placeholder="请选择"
        style="width: 100px"
        clearable
        @change="selectGrade($event)"
      >
        <el-option
          v-for="item in grade"
          :key="item.value"
          :label="item.name"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span style="margin-left: 20px">班级：</span>
      <el-select
        v-model="clazzId"
        placeholder="请选择"
        style="width: 100px"
        clearable
        @change="selectClazz"
      >
        <el-option
          v-for="items in clazz"
          :key="items.value"
          :label="items.name"
          :value="items.id"
        >
        </el-option>
      </el-select>
    </div>
    <div style="margin: 10px 0; width: 500px; float: right;margin-right:20px">
      <el-button
        type="warning"
        style="margin-left: 20px; float: right"
        @click="deleteBatch"
        icon="el-icon-delete"
        >批量删除</el-button
      >
      
      <el-button
        type="primary"
        style="margin-left: 20px; float: right"
        @click="add"
        icon="el-icon-plus"
        >添加</el-button
      >
    </div>

    <!--  表格-->
    <el-table :data="tableData" border stripe style="width: 99%" height="76vh">
      
      <el-table-column prop="headmasterId" label="教工号" />
      <el-table-column prop="headmasterName" label="姓名" />
      <el-table-column prop="sex" label="性别" />
      <el-table-column prop="age" label="年龄" />
      <el-table-column prop="phone" label="电话" />
      <el-table-column prop="gradeName" label="年级" />
      <el-table-column prop="clazzName" label="班级" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            @click="handleEdit(scope.row, scope.row.gradeId)"
            size="mini"
            type="primary"
            plain
            >编辑</el-button
          >
          <el-button
            @click="handleDelete(scope.row.id)"
            type="danger"
            size="mini"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加班主任信息"
      v-model="dialogVisible"
      width="50%"
      :before-close="dialogClose"
    >
      <el-form :model="form" ref="form" :rules="rules">
        <el-row>
          <el-col span="12">
            <el-form-item label="教工号" prop="headmasterId" label-width="100px">
              <el-input v-model="form.headmasterId" placeholder="请输入教工号" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="姓名" prop="headmasterName" label-width="100px">
              <el-input v-model="form.headmasterName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="性别" prop="sex" label-width="100px">
              <el-radio v-model="form.sex" label="男">男</el-radio>
              <el-radio v-model="form.sex" label="女">女</el-radio>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="年龄" prop="age" label-width="185px">
              <el-input v-model.number="form.age" placeholder="请输入年龄" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="年级" prop="gradeId" label-width="100px">
              <el-select
                v-model="form.gradeId"
                placeholder="请选择年级"
                style="width: 190px"
                @change="selectGrade($event)"
              >
                <el-option
                  v-for="item in grade"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="班级" prop="clazzId" label-width="100px">
              <el-select
                v-model="form.clazzId"
                placeholder="请选择班级"
                style="width: 190px"
              >
                <el-option
                  v-for="items in clazz"
                  :key="items.value"
                  :label="items.name"
                  :value="items.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="电话" prop="phone" label-width="100px">
              <el-input
                v-model="form.phone"
                placeholder="请输入电话"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogClose">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </span>
      </template>
    </el-dialog>

    <!--        更新学生信息-->
    <el-dialog title="编辑信息" v-model="updateVisible" width="50%">
      <el-form :model="form" ref="form">
        <el-row>
          <el-col span="12">
            <el-form-item label="学号" prop="headmasterId" label-width="100px">
              <el-input v-model="form.headmasterId" placeholder="请输入教工号" />
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="姓名" prop="headmasterName" label-width="100px">
              <el-input v-model="form.headmasterName" placeholder="请输入姓名" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="性别" prop="sex" label-width="100px">
              <el-radio v-model="form.sex" label="男">男</el-radio>
              <el-radio v-model="form.sex" label="女">女</el-radio>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="年龄" prop="age" label-width="185px">
              <el-input v-model="form.age" placeholder="请输入年龄" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="年级" prop="gradeId" label-width="100px">
              <el-select
                v-model="form.gradeId"
                placeholder="请选择年级"
                style="width: 190px"
                @change="selectGrade($event)"
              >
                <el-option
                  v-for="item in grade"
                  :key="item.value"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col span="12">
            <el-form-item label="班级" prop="clazzId" label-width="100px">
              <el-select
                v-model="form.clazzId"
                placeholder="请选择班级"
                style="width: 190px"
              >
                <el-option
                  v-for="items in clazz"
                  :key="items.value"
                  :label="items.name"
                  :value="items.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col span="12">
            <el-form-item label="电话" prop="phone" label-width="100px">
              <el-input
                v-model="form.phone"
                placeholder="请输入电话"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateVisible = false">取 消</el-button>
          <el-button type="primary" @click="update">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "../../utils/request";
export default {
  name: "Headmaster",
  components: {},
  data() {
    return {
      rules: {
        headmasterId: [
            {required: true, message: '教工号不得为空', trigger: 'blur'},
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
            
        ],
        headmasterName: [
            {required: true, message: '请输入姓名', trigger: 'blur'},
        
        ],
        sex:[
            {required:true,message:'请选择性别',trigger:'blur'}
        ],
        
        phone:[
            { required: true, trigger: 'blur', validator: this.phoneValidator }
        ],
        gradeId:[
            {required:true,message:'请选择年级',trigger:'blur'}
        ],
        age:[
            {required:true,message:'请输入年龄',trigger:'blur'},
            { type: 'number', message: '年龄必须是数值',trigger: 'blur'}
        ],
        clazzId:[
            {required:true,message:'请选择班级',trigger:'blur'}
        ],
      },

      //当前页
      currentPage: 1,
      pageSize: 10,
      total: 0,
      //弹窗默认关闭
      dialogVisible: false,
      updateVisible: false,
      search: "",
      gradeId: null,
      clazzId: null,
      form: {},
      tableData: [],
      grade: [],
      clazz: [],
    };
  },
  created() {
    this.findGrade();
    this.load();
  },
  //定义函数
  methods: {
    phoneValidator(rule, value, callback) {
      const phoneReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[189])\d{8}$/
      if (!value) {
        callback(new Error('不能为空'))
      } else {
        // 正则表达式判断是否符合格式
        if (phoneReg.test(value)) {
          // 如果符合
          callback()
        } else {
          callback(new Error('手机号码格式错误'))
          console.log('手机号码格式错误')
        }
      }
    },
    // 查询年级信息
    findGrade() {
      request.get("/grade/all", {}).then((res) => {
        console.log(res);
        this.grade = res.data;
        console.log("1111111");
        console.log(this.grade[0].name);
      });
    },
    // 查询班级信息
    findClazz() {
      request.get("/clazz/all", {}).then((res) => {
        console.log(res);
        this.clazz = res.data;
        console.log("1111111");
        console.log(this.clazz[0].name);
      });
    },
    // 查询方法
    load() {
      request
        .get("/headmaster/page", {
          params: {
            PageNum: this.currentPage,
            PageSize: this.pageSize,
            headmasterName: this.search,
            gradeId: this.gradeId,
            clazzId: this.clazzId,
          },
        })
        .then((res) => {
          console.log(res);
          this.tableData = res.data.records;
          this.total = res.data.total;
        //   // console.log(res.data.records[2].classList[0].className)
        //   console.log(res.data.records);
          for (let i = 0; i <= res.data.records.length; i++) {
            this.tableData[i].gradeName = res.data.records[i].grade.name;
            this.tableData[i].clazzName = res.data.records[i].clazz.name;
          }
        });
    },
    //新增学生
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          request.post("/headmaster/add", this.form).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "添加成功",
              });
              this.dialogVisible = false;
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
            this.load();
          });
        }
      });
    },
    //更新班主任信息
    update() {
      request.put("/headmaster/update", this.form).then((res) => {
        console.log(res);
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "更新成功",
          });
          this.updateVisible = false;
        } else {
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
        this.load();
      });
    },
    //删除学生
    handleDelete(id) {
      // alert(id)
      console.log(id);
      this.$confirm("是否删除该班主任信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          request.delete("/headmaster/delete/" + id).then((res) => {
            if (res.code === "0") {
              this.$message({
                type: "success",
                message: "删除成功",
              });
            } else {
              this.$message({
                type: "error",
                message: res.msg(),
              });
            }
            this.load();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //改变当前每页的个数触发
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    //改变当前页码触发
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum;
      this.load();
    },
    //打开dialogVisible弹窗
    add() {
      this.dialogVisible = true;
      //清空弹窗的内容
      this.form = {};
    },
    //打开编辑弹框
    handleEdit(row, gradeId) {
      console.log(row);
      console.log(gradeId);
      request
        .get("/clazz/findClazzByGid", {
          params: {
            gradeId: gradeId,
          },
        })
        .then((res) => {
          console.log(res);
          this.clazz = res.data;
          console.log("1111111");
          console.log(this.clazz[0].name);
        });
      this.form = JSON.parse(JSON.stringify(row));
      this.updateVisible = true;
    },
    // 关闭弹窗事件
    dialogClose() {
      //清空表单验证信息
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    selectGrade($event) {
      // alert($event)
      this.clazzId = null;
      this.form.clazzId = null;
      let gradeId = $event;
      console.log($event);

      request
        .get("/clazz/findClazzByGid", {
          params: {
            gradeId: gradeId,
          },
        })
        .then((res) => {
          console.log(res);
          this.clazz = res.data;
          console.log("1111111");
          console.log(this.clazz[0].name);
        });
      // console.log(a);
      this.load();
    },
    selectClazz() {
      this.load();
    },
  },
};
</script>
